package cn.itsource.pet.service.impl;

import cn.itsource.org.domain.dto.OrderConfirmDto;
import cn.itsource.pet.domain.OrderPetAcquisition;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.service.IOrderPetAcquisitionService;
import cn.itsource.basic.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huang
 * @since 2022-07-23
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    public void confirm(OrderConfirmDto dto) {
        //修改收购单的支付方式和支付金额
        OrderPetAcquisition opa = orderPetAcquisitionMapper.loadById(dto.getId());
        opa.setPaytype(dto.getPayType());
        opa.setPrice(dto.getMoney());
        //修改收购单状态为已完成
        opa.setState(1);
        orderPetAcquisitionMapper.update(opa);
        //修改寻主消息的状态为已完成
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(opa.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
    }

    @Override
    public void cancel(Long id) {
        //更新收购单状态为取消
        OrderPetAcquisition opa = orderPetAcquisitionMapper.loadById(id);
        opa.setState(-1);
        orderPetAcquisitionMapper.update(opa);

        //置空寻主消息的shopid，回归寻主池
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(opa.getSearchMasterMsgId());
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);
    }
}
